$(document).ready(function()
    {
        var angsuran;
        var piutang;
        var spinner;
        var status_spinner;
        
        $('.date-picker').datepicker( {
            changeYear: true,
            changeMonth: true,
            showWeek: true,
            showButtonPanel: true,
            dateFormat: 'yy-mm',
            onClose: function(dateText, inst) {
                var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
                var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
                $(this).datepicker('setDate', new Date(year, month, 1));
            }
        });

        $.generate_potongan = function(){
            status_spinner = true;
            $.loading();
            if($("#TGL_ANGSURAN").val() != "")
                $.data_ajax('br-03/potongan/generate_potongan/', "#FORM_BUAT_POTONGAN", $.notif);
            
        }

        $.notif = function(data){
            $("#siteLoader").hide();
            status_spinner = false;
            spinner.stop();
            $.loading();
            var simpanan = data.data['simpananwajib'];
            var pinjaman = data.data['pinjaman'];
            if (data.data['nodata']!=''){
                alert(data.data['nodata']);
            }else if (simpanan!='' || pinjaman!=''){
                var respon = simpanan + ' \n' + pinjaman;
                alert(respon);
            }
        }

        $.loading = function(){
            var opts = {
              lines: 12, // The number of lines to draw
              length: 30, // The length of each line
              width: 6, // The line thickness
              radius: 24, // The radius of the inner circle
              color: '#000', // #rgb or #rrggbb
              speed: 1, // Rounds per second
              trail: 70, // Afterglow percentage
              shadow: false // Whether to render a shadow
            };
            var target = document.getElementById('siteLoader');
            spinner = new Spinner(opts);
            
            if (status_spinner) {
                $("#siteLoader").show();
                spinner.spin(target);
            }
               else if (!status_spinner) {
                   $("#siteLoader").hide();
                   spinner.stop();
               }

            
        }

        $("#CREATE").click($.generate_potongan);

        $("#FORM_BUAT_POTONGAN").submit(function(){
            return false;
        })

        $("#siteLoader").hide();

        

    });